package com.example.springboot.json;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringbootJsonApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootJsonApplication.class, args);
	}

	/**
	 * 使用bean方式注入fastjson解析器
	 *
	 * @return
	 */
	@Bean
	public HttpMessageConverters fastJsonHttpMessageConverters() {
		// 创建fastjson对象
		FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();

		FastJsonConfig confg = new FastJsonConfig();
		// 设置是否需要格式化
		confg.setSerializerFeatures(SerializerFeature.PrettyFormat);
		converter.setFastJsonConfig(confg);
		return new HttpMessageConverters(converter);
	}

}



///**
// * 1.使用fastjson,需要继承WebMvcConfigurerAdapter,覆盖configureMessageConverters方法
// *
// * @author Administrator
// *
// */
//@SpringBootApplication
//public class SpringbootJsonApplication extends WebMvcConfigurerAdapter {
//
//	public static void main(String[] args) {
//		// 启动spring容器
//		SpringApplication.run(App.class, args);
//	}
//
//	@Override
//	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//		super.configureMessageConverters(converters); // 创建fastjson对象
//		//创建convert消息转换对象
//		FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//
//		// 添加fastjosn配置信息，设置是否需要格式化
//		FastJsonConfig confg = new FastJsonConfig();
//		confg.setSerializerFeatures(SerializerFeature.PrettyFormat);
//		//添加配置信息到消息对象
//		converter.setFastJsonConfig(confg);
//
//		converters.add(converter);
//	}
//
//}
